package com.boot.apollo.modules.guava;

import com.google.common.util.concurrent.SimpleTimeLimiter;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class SimpleTimeLimiterDemo {

    public static void main(String[] args) {

        // https://blog.csdn.net/timchen525/article/details/82903110
        SimpleTimeLimiter simpleTimeLimiter = new SimpleTimeLimiter();
        final AtomicBoolean isInterrupted = new AtomicBoolean();

        Callable<String> task = new Callable<String>() {
            @Override
            public String call() throws Exception {
                isInterrupted.set(true);
                Thread.sleep(200);
                isInterrupted.set(false);
                return "success";
            }
        };

        try {
            String result = simpleTimeLimiter.callWithTimeout(task, 200, TimeUnit.MILLISECONDS, true);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println("异常：" + e.getMessage());
            e.printStackTrace();
        }
    }







}
